package class8;

import common.TreeNode;
import common.TreeNodeUtils;

/**
 * 二叉树先序和后续 (递归方式)
 *
 *      a
 *    b    c
 *  d  e  x  g
 *    t  k s
 *
 */
public class Code01_BinaryTreeX {
    public static void main(String[] args) {
        TreeNode treeNode1 = TreeNodeUtils.createTreeNode(1, 2, 3);
        TreeNode left = treeNode1.left;
        TreeNode right = treeNode1.right;
        TreeNode[] t3 = TreeNodeUtils.getTreeNodes(4, 5, 6, 7);
        TreeNode[] t4 = TreeNodeUtils.getTreeNodes(8, 9, 10);

        left.left = t3[0];
        left.right = t3[1];
        right.left = t3[2];
        right.right = t3[3];

        t3[1].left = t4[0];
        t3[2].left = t4[1];
        t3[2].right = t4[2];

        System.out.print("先序：");
        pre(treeNode1);


        System.out.println();
        System.out.print("后序：");
        pos(treeNode1);
    }

    public static void f(TreeNode cur) {
        if (cur == null) return;
        f(cur.left);
        f(cur.right);
    }

    // 1.前序遍历
    public static void pre(TreeNode cur) {
        if (cur == null) return;
        System.out.print(getChar(cur.val) + "\t");
        pre(cur.left);
        pre(cur.right);
    }

    // 2.中序遍历
    public static void mid(TreeNode cur) {
        if (cur == null) return;
        mid(cur.left);
        System.out.print(getChar(cur.val) + "\t");
        mid(cur.right);
    }

    // 3.后序遍历
    public static void pos(TreeNode cur) {
        if (cur == null) return;
        pos(cur.left);
        pos(cur.right);
        System.out.print(getChar(cur.val) + "\t");

    }
    public static char getChar(int index){
        String str = "0abcdeXgtks";
        return str.charAt(index);
    }
}
